import { _decorator, Component, Node, Label, director } from 'cc';
import { SSS_Consts } from "../../../Script/Data/SSS_Consts";
import { ShuZiHuaRongDao_GameData } from "./ShuZiHuaRongDao_GameData";
import { ShuZiHuaRongDao_GameStorage } from "./ShuZiHuaRongDao_GameStorage";
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';

const { ccclass, property } = _decorator;

@ccclass('ShuZiHuaRongDaoGamePanel')
export default class ShuZiHuaRongDao_GamePanel extends Component {
    static Instance: ShuZiHuaRongDao_GamePanel;
    @property(Node)
    Button_back: Node | null = null;
    @property(Node)
    Button_refresh: Node | null = null;
    @property(Label)
    Label_best_time: Label | null = null;
    @property(Label)
    Label_mode: Label | null = null;
    @property(Label)
    Label_step_number: Label | null = null;
    onLoad() {
        ShuZiHuaRongDao_GamePanel.Instance = this;
    }
    ShowOn() {
        this.node.active = true;
        this.AddListener();
        this.Refresh();
    }
    ShowOff() {
        this.node.active = false;
        this.RemoveListener();
    }
    Refresh() {
        this.Refresh_score();
        this.Refresh_mode();
    }
    Refresh_mode() {
        if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_3x3`) {
            this.Label_mode.string = `3x3`;
        }
        else if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_4x4`) {
            this.Label_mode.string = `4x4`;
        }
        else if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_5x5`) {
            this.Label_mode.string = `5x5`;
        }
        else if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_6x6`) {
            this.Label_mode.string = `6x6`;
        }
        else if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_7x7`) {
            this.Label_mode.string = `7x7`;
        }
        else if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_8x8`) {
            this.Label_mode.string = `8x8`;
        }
        else if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_9x9`) {
            this.Label_mode.string = `9x9`;
        }
        else if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_10x10`) {
            this.Label_mode.string = `10x10`;
        }
    }
    Refresh_score() {
        // this.Label_best_time.string = `${ShuZiHuaRongDao_GameStorage.data[ShuZiHuaRongDao_GameData.Game_mode].best_time}`;
        this.Label_best_time.string = ShuZiHuaRongDao_GameData.Get_time_format(ShuZiHuaRongDao_GameStorage.data[ShuZiHuaRongDao_GameData.Game_mode].best_time);

        this.Label_step_number.string = `${ShuZiHuaRongDao_GameStorage.data[ShuZiHuaRongDao_GameData.Game_mode].current_data.step_number}`;
    }
    AddListener() {
        this.Button_back.on(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.on(Node.EventType.TOUCH_END, this.On_Button_refresh, this);

    }
    RemoveListener() {
        this.Button_back.off(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.off(Node.EventType.TOUCH_END, this.On_Button_refresh, this);
    }
    On_Button_back() {
        director.loadScene(SSS_Consts.SceneName.ShuZiHuaRongDao_Main_Scene);

        SSS_AudioManager.PlayButtonAudio();
    }
    On_Button_refresh() {
        ShuZiHuaRongDao_GameData.ClearData();

        let currentSceneName = director.getScene().name;
        director.loadScene(currentSceneName);

        SSS_AudioManager.PlayButtonAudio();
    }
}

// /** 调试 */
window["ShuZiHuaRongDao_GamePanel"] = ShuZiHuaRongDao_GamePanel;